package com.isecommerce.application.datawizard;

import java.io.PrintWriter;
import java.util.*;

import com.isecommerce.framework.util.Config;
import com.isecommerce.framework.util.LogWriter;
import com.isecommerce.application.datawizard.entity.*;

/**
 * RAW CSV File Meta Info Manager<br><br>
 *
 * This class cache the RAW CSV file meta info from 'conf.proeprties'.
 * 
 *
 * @version 1.2.5
 * @author Leslie Chen
 */
// History

public class RAWCSVFileInfoManager {
    
    protected Hashtable<String,RAWCSVFileInfoEntity> infoTable;
    protected ArrayList<RAWCSVFileInfoEntity>        infoArray;
    protected int csvFileCount;
    
    private LogWriter logger;
            
    /**
     * Initializing manager from application configuration file.
     *
     */
    public void init(Config config){
    	
    	// Init logger
    	this.logger = LogWriter.getLogWriterInstance("DATA_WIZARD", LogWriter.INFO, new PrintWriter(System.out));
    	this.logger.setOwner(this.getClass().getSimpleName());
    	
    	this.csvFileCount = Integer.parseInt(config.getValue("raw.csvfile.count").trim());    	
        infoTable = new Hashtable<String,RAWCSVFileInfoEntity>();
        infoArray = new ArrayList<RAWCSVFileInfoEntity>();
                
        RAWCSVFileInfoEntity info = null;
        RAWCSVColumnInfoEntity colInfo = null;
        ArrayList<RAWCSVColumnInfoEntity> arrCol = null;
        
        String id = "";
        String col_id = "";
    	
        for(int i=1; i<=this.csvFileCount; i++) {
            id = String.valueOf(i);
            
            arrCol = new ArrayList<RAWCSVColumnInfoEntity>();
            
            try{
            	
            	// Get Column info
            	for(int col_cnt=1; col_cnt<=Integer.parseInt(config.getValue("raw.csv." + id + ".column.count"));col_cnt++){
        			col_id = String.valueOf(col_cnt);
        			
        			colInfo = new RAWCSVColumnInfoEntity(config.getValue("raw.csv." + id + ".column." + col_id + ".name"),
        					        Boolean.parseBoolean(config.getValue("raw.csv." + id + ".column." + col_id + ".value.trim").trim()),
        					        Boolean.parseBoolean(config.getValue("raw.csv." + id + ".column." + col_id + ".value.remove_LF").trim()),
        					                             config.getValue("raw.csv." + id + ".column." + col_id + ".value.replace"),
        					                             config.getValue("raw.csv." + id + ".column." + col_id + ".value.remove_char"),
        					        Boolean.parseBoolean(config.getValue("raw.csv." + id + ".column." + col_id + ".value.multiple_val").trim()),
        					        					 config.getValue("raw.csv." + id + ".column." + col_id + ".value.multiple_val.delimiter"));
        			
        			if(colInfo.colName != null){
        				
        				arrCol.add(colInfo);        			
        				this.logger.log("[RAW CSV COL_INFO] " + col_cnt + ": '" + colInfo.colName + "' registered.", LogWriter.INFO);
            			
        			}else this.logger.log("[RAW CSV COL_INFO] " + col_cnt + ": skipped.", LogWriter.INFO);
        			
        			
            	}
            	
            	// Get CSV file info
               	info = new RAWCSVFileInfoEntity(config.getValue("raw.csv." + id + ".csvname"),
     		               			            config.getValue("raw.csv." + id + ".filename"),
     		                   Integer.parseInt(config.getValue("raw.csv." + id + ".column.count").trim()),
                                                config.getValue("raw.csv." + id + ".column.delimiter"),
                                                config.getValue("raw.csv." + id + ".encode"),
                               Integer.parseInt(config.getValue("raw.csv." + id + ".skipline").trim()),
                                                config.getValue("raw.csv." + id + ".endofline"),
                           Boolean.parseBoolean(config.getValue("raw.csv." + id + ".enable").trim()),
                                                arrCol);
               	
               	infoTable.put(info.csvName, info);
               	infoArray.add(info);
     
               	this.logger.log("[OK] '" + info.csvName + "' RAW CSV File Meta Info registered.", LogWriter.INFO);
               	this.logger.log("[DELIMITER] '" + info.colDel + "'", LogWriter.DEBUG);
               	
            }catch(Exception e){
            	this.logger.log(e, "Properties not found", LogWriter.ERROR);
            }
            
 
           	                     
            
        }         	
        
        this.logger.log(infoArray.size() + " CSV FileInfo registered successfully.", LogWriter.INFO);
    }
    
    /**
     * Get csv file info by given csv alias name.
     *
     * @param name Given csv alias name.
     * @return csv file info entity.
     */
    public RAWCSVFileInfoEntity getCSVfileInfoByKey(String csvname){
        
        return infoTable.get(csvname);
    }  
    
    /**
     * Get csv file info by given index.
     *
     * @param index Given index number.
     * @return csv file info entity.
     */
    public RAWCSVFileInfoEntity getCSVfileInfoByIndex(int index){
        
        return infoArray.get(index);
    }
    
    public int getFileCount(){
    	return this.csvFileCount;    	
    }
}
